﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        int createdTab = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {



        }


        private void openFile()
        {
            /* Open File Function */

            openFileDialog1.InitialDirectory = "C:";
            openFileDialog1.Title = "Open a File";
            openFileDialog1.FileName = "";

            if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                frmChild childForm = (frmChild)this.ActiveMdiChild;
                string Chosen_File = System.IO.File.ReadAllText(openFileDialog1.FileName);
                childForm.scintilla1.Text = Chosen_File;
            }
        }

        private void saveFile()
        {
            /* Save File Function */

            saveFileDialog1.InitialDirectory = "C:";
            saveFileDialog1.Title = "Save a File";
            saveFileDialog1.FileName = "";

            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                //save file
                frmChild childForm = (frmChild)this.ActiveMdiChild;
                string Saved_File = saveFileDialog1.FileName;
                TextWriter tw = new StreamWriter(Saved_File);

                tw.WriteLine(childForm.scintilla1.Text);

                tw.Close();
            }
        }

        private void newFile()
        {
            frmChild newChild = new frmChild();      //add new child
            TabPage childTab = new TabPage();         //create new tab page
            newChild.MdiParent = this;                       //set as child of this form
            newChild.Name = "File" + createdTab.ToString();
            newChild.Text = " File " + createdTab.ToString();
            childTab.Name = newChild.Name;           //make sure name and text are same
            childTab.Text = newChild.Text;                  //this is for syncrhonize later
            tabControl1.TabPages.Add(childTab);     //add new tab
            newChild.scintilla1.Parent = childTab;     //attach to tab
            ToolStripMenuItem newMenuTab = new ToolStripMenuItem();   //create menu to hold tab
            newMenuTab.Text = newChild.Text;                    //make sure the name and text are same to synchonize later
            newMenuTab.Name = newChild.Name;
            //mnuTab.DropDownItems.Add(newMenuTab);    //add menu item
            //newMenuTab.Click += new EventHandler(newMenuTab_Click); //add event handler
            tabControl1.SelectTab(childTab);     //this is to make sure that tab page is selected in the same time
            newChild.Show();                                 //as new form created so that corresponding tab and child form is active
            createdTab++;   //increment of course
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            foreach (frmChild WantToSelect in this.MdiChildren)
            {
                if (tabControl1.SelectedTab != null)     //if no child has created, an error will occur
                {
                    if (WantToSelect.Name == tabControl1.SelectedTab.Name)    //again, this is why you must make sure child form's and tab page's name are same
                    {                                                                           //for easier control
                        WantToSelect.Select();                                //activate mdi child
                    }
                }
            }
        }

        private void sUndo()
        {
            //undocode
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.UndoRedo.Undo();
        }

        private void sRedo()
        {
            //redo code
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.UndoRedo.Redo();
        }

        private void sClipboard(int cond)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            if (cond == 1)
                childForm.scintilla1.Clipboard.Cut();
            if (cond == 2)
                childForm.scintilla1.Clipboard.Copy();
            if (cond == 3)
                childForm.scintilla1.Clipboard.Paste();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            openFile();
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            saveFile();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFile();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFile();
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            newFile();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            newFile();
        }

        private void cutToolStripButton_Click(object sender, EventArgs e)
        {
            sClipboard(1);
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sUndo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sRedo();
        }

        private void undoToolStripButton_Click(object sender, EventArgs e)
        {
            sUndo();
        }

        private void redoToolStripButton_Click(object sender, EventArgs e)
        {
            sRedo();
        }

        private void copyToolStripButton_Click(object sender, EventArgs e)
        {
            sClipboard(2);
        }

        private void pasteToolStripButton_Click(object sender, EventArgs e)
        {
            sClipboard(3);
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sClipboard(1);
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sClipboard(2);
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sClipboard(3);
        }

        private void newToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            newFile();
        }

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            openFile();
        }

        private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            saveFile();
        }

        private void undoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            sUndo();
        }

        private void redoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            sRedo();
        }

        private void cutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            sClipboard(1);
        }

        private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            sClipboard(2);
        }

        private void pasteToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            sClipboard(3);
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.Selection.SelectAll();
        }

        void scintilla1_CharAdded(object sender, ScintillaNet.CharAddedEventArgs e)
        {
            if (Variables.AutoCEnable == true)
            {
                frmChild childForm = (frmChild)this.ActiveMdiChild;
                childForm.scintilla1.AutoComplete.Show();
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void enableAutocompleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Variables.AutoCEnable = true;
        }

        private void disableAutocompleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Variables.AutoCEnable = false;
        }

        private void enableLineNumbersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.Margins[0].Width = 20;
        }

        private void disableLineNumbersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.Margins[0].Width = 0;
        }

        private void helpToolStripButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("(c) Softwave 2012", "About Oasis Text",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void asmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.ConfigurationManager.Language = "asm";
        }

        private void cToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.ConfigurationManager.Language = "cpp";
        }


        private void cToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.ConfigurationManager.Language = "cs";
        }


        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("(c) Softwave 2012", "About Oasis Text",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void exportToHTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void hTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.ConfigurationManager.Language = "html";
        }

        private void aSMToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.ConfigurationManager.Language = "asm";
        }

        private void javascriptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.ConfigurationManager.Language = "js";
        }

        private void pythonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmChild childForm = (frmChild)this.ActiveMdiChild;
            childForm.scintilla1.ConfigurationManager.Language = "python";
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ActiveMdiChild.Close();
            tabControl1.SelectedTab.Dispose();
        }

    }
    public class Variables
    {
        /* Variables */
        public static bool AutoCEnable = false;
    }
}
